package com.xumz.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.xumz.model.ClassName;
import com.xumz.model.Student;
import com.xumz.model.StudentManage;
import com.xumz.service.ClassService;
import com.xumz.service.StudentService;

@Controller
public class StudentController {
	@Autowired
	StudentService cms;
	@Autowired
	ClassService classService;
	
	public StudentController() {
		System.out.println("StudentCsontroller");
	}
	
	@RequestMapping(value = "/stumemberDelete/{stuid}",method=RequestMethod.GET)
	public String stumemberDelete(HttpServletRequest request, HttpServletResponse response,
			Model model,@PathVariable("stuid") String stuid) throws UnsupportedEncodingException{
		if (cms.stumemberDelete(stuid) == true){
			request.setAttribute("studelete", "1");
//			JOptionPane.showMessageDialog(null, "删除成功"); 
		} else {
//			JOptionPane.showMessageDialog(null, "删除失败"); 
		}
		return "teacher/admin/classgroup/index";
	}
	
	@RequestMapping(value = "classinformationUpdate",method = {RequestMethod.POST})
	public String classinformationupdate(HttpServletRequest request, HttpServletResponse response,
			Model model, @RequestParam("stuid")String stuid,
			 @RequestParam("updatename")String updatename) throws UnsupportedEncodingException{
		if (updatename == "") {
			stuinformation(request,model, stuid);
		} else {
			Student student = new Student(stuid, updatename, null, null);
			if (cms.classinformationUpdate(student) == true) {
				request.setAttribute("classupdate", "1");
				System.out.println("request:" + request.getAttribute("classupdate"));
//				JOptionPane.showMessageDialog(null, "修改完毕"); 
				stuinformation(request,model, stuid);
			} else {
//				JOptionPane.showMessageDialog(null, "修改失败"); 
			}
		}
		return "teacher/admin/student/information";
	}
	
	@RequestMapping(value = "bloginformationUpdate",method = {RequestMethod.POST})
	public String bloginformationupdate(HttpServletRequest request, HttpServletResponse response,
			Model model, @RequestParam("stuid")String stuid,
			 @RequestParam("updatename")String updatename) throws UnsupportedEncodingException{
		if (updatename == "") {
			stuinformation(request,model, stuid);
		} else {
			Student student = new Student(stuid, null, null, updatename);
			if (cms.bloginformationUpdate(student) == true) {
				request.setAttribute("blogupdate", "1");
//				JOptionPane.showMessageDialog(null, "修改完毕"); 
				stuinformation(request,model, stuid);
			} else {
//				JOptionPane.showMessageDialog(null, "修改失败"); 
			}
		}
		return "teacher/admin/student/information";
	}
	
	@RequestMapping(value = "stuinformationUpdate",method = {RequestMethod.POST})
	public String stuinformationupdate(HttpServletRequest request, HttpServletResponse response,
			Model model, @RequestParam("stuid")String stuid,
			 @RequestParam("updatename")String updatename) throws UnsupportedEncodingException{
		if (updatename == "") {
			stuinformation(request,model, stuid);
		} else {
			Student student = new Student(stuid, updatename);
			if (cms.stuinformationUpdate(student) == true) {
				request.setAttribute("stuupdate", "1");
//				JOptionPane.showMessageDialog(null, "修改完毕"); 
				stuinformation(request,model, stuid);
			} else {
//				JOptionPane.showMessageDialog(null, "修改失败"); 
			}
		}
		return "teacher/admin/student/information";
	}
	
	@RequestMapping(value = "/stuinformation/{stuid}",method=RequestMethod.GET)
	public String stuinformation(HttpServletRequest request,Model model,@PathVariable("stuid") String stuid) throws UnsupportedEncodingException{
		String name=(String) request.getSession().getAttribute("name");
		Student student = cms.findStudentById(stuid);
		String classname = classService.classNameById(student.getClassId());
		StudentManage stu = new StudentManage(student.getStuId(), student.getClassId(), student.getStuName(), student.getBlogAddress(), 
				student.getBlogNumber(), student.getStuFans(), student.getStuFocus(), classname);
		model.addAttribute("stu", stu);
		List<ClassName> list = classService.selectClassNameByTeacherName(name);
		model.addAttribute("list",list);
		return "teacher/admin/student/information";
	}
	
	@RequestMapping("stumembersadd")
	public String stumembersadd(HttpServletRequest request, HttpServletResponse response,
			Model model, @RequestParam("stuname")String stuname,
			 @RequestParam("blogurl")String blogurl,@RequestParam("classoption")String classoption){
		List<ClassName> listClass = classService.selectClassName();
		model.addAttribute("listClass",listClass);
		if (cms.stumembersadd(stuname,blogurl,classoption) == true){
			request.setAttribute("stuupdate", "1");
//			JOptionPane.showMessageDialog(null, "学生" + stuname + "已成功录入"); 
		} else {
//			JOptionPane.showMessageDialog(null, "录入失败"); 
		}
		return "teacher/admin/student/add";
	}
	
	@RequestMapping(value = "studentGroupByClass",method = {RequestMethod.POST})
	public String studentGroupByClass(HttpServletRequest request, HttpServletResponse response,
			Model model, @RequestParam("classoption")String classoption,
			@RequestParam("studentName")String studentName){
		String name=(String) request.getSession().getAttribute("name");
		List<ClassName> classNameList = classService.selectClassNameByTeacherName(name);
		model.addAttribute("classNameList",classNameList);
		
		List<StudentManage> list=cms.groupStudentBySerch(name,classoption,studentName);
		model.addAttribute("list",list);
		
		String className=classService.classNameById(classoption);
		if (!classoption.equals("所有班级")) {
			model.addAttribute("classId",classoption);
			model.addAttribute("className",className);
		}
		if (!studentName.equals("")) {
			model.addAttribute("studentName",studentName);
		}
		return "teacher/admin/classgroup/index";
	}
	
}
